package com.gp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gp.common.utils.CookieUtils;
import com.gp.common.utils.StringUtil;
import com.gp.mapper.BaseCouponDistributionMapper;
import com.gp.mapper.BaseOrderMapper;
import com.gp.mapper.BaseOrdergoodsMapper;
import com.gp.mapper.BaseUserExtendsMapper;
import com.gp.mapper.BaseUserrelationMapper;
import com.gp.model.dto.AlbApplyVo;
import com.gp.model.dto.Count;
import com.gp.model.dto.OrderDetailWithGoodsDto;
import com.gp.model.dto.OrderGoodsVO;
import com.gp.model.dto.PayInfoVo;
import com.gp.model.entity.BaseAlbApply;
import com.gp.model.entity.BaseCouponDistribution;
import com.gp.model.entity.BaseOrder;
import com.gp.model.entity.BaseOrdergoods;
import com.gp.model.entity.BaseUser;
import com.gp.model.entity.BaseUserExtends;
import com.gp.model.entity.BaseUserrelation;
import com.gp.model.entity.SubmitInfoVo;
import com.gp.service.IBaseOrderService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 弓成龙
 * @since 2017-12-19
 */
@Service
public class BaseOrderServiceImpl extends ServiceImpl<BaseOrderMapper, BaseOrder>
    implements IBaseOrderService {

  @Autowired
  private BaseOrderMapper orderMapper;
  @Autowired
  private BaseCouponDistributionMapper coponMapper;
  @Autowired
  private BaseUserrelationMapper relationMapper;
  @Autowired
  private BaseOrdergoodsMapper ordergoodsMapper;
  @Autowired
  private BaseUserExtendsMapper userExtendsMapper;

  @Override
  public Page<BaseOrder> getOrderList(Page page, Long uid) {
    Map map = page.getCondition();
    map = map == null ? new HashMap() : map;
    map.put("userId", uid);
    return page.setRecords(orderMapper.getOrderList(page, map));
  }

  @Override
  public boolean cancelOrder(Long id) {
    BaseOrder entity = orderMapper.selectById(id);
    entity.setOrderState("已取消");
    return orderMapper.updateById(entity) == 1;
  }

  @Override
  public boolean addOrder(BaseOrder order) {
    // 设置订单所使用的优惠券失效
    BaseCouponDistribution coupon = coponMapper.selectById(order.getCouponCode());
    coupon.setIsvalid("1"); // 1： 已使用 0： 未使用
    coponMapper.updateById(coupon);
    return orderMapper.insert(order) == 1;
  }

  @Override
  public boolean pay(PayInfoVo payInfo) {
    if (submit(payInfo.getSubmitInfo()) && payMoney(payInfo.getAmount())) {
      Map<String, Object> map = new HashMap();
      map.put("ORDERCODE", (payInfo.getOrderId()));
      BaseOrder order = ((List<BaseOrder>) orderMapper.selectByMap(map)).get(0);
      order.setPayfee("" + payInfo.getAmount());
      order.setPayState(1); // 已支付
      order.setPayType(payInfo.getPayType());
      order.setPayTime(new Date());
      return orderMapper.updateById(order) == 1;
    }
    return false;
  }

  /**
   * 支付-- 扣除艾乐币
   */
  public boolean payMoney(int count) {
    Long uid = ((BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest())).getId();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("USERID", uid);
    // 2. 用户账户减少对应的金额
    BaseUserExtends user = ((List<BaseUserExtends>) userExtendsMapper.selectByMap(map)).get(0);
    int money = user.getCountMany();
    user.setCountMany(money - count);
    if (money - count < 0 || userExtendsMapper.updateById(user) != 1) {
      return false;
    }
    return true;
  }

  @Override
  public boolean submit(SubmitInfoVo submitInfo) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("ORDERCODE", submitInfo.getOrderCode());
    //
    List<BaseOrder> list = orderMapper.selectByMap(map);
    if (list == null || list.isEmpty())
      return false;
    BaseOrder order = list.get(0);
    order.setOrderState("已完成");
    order.setOrderValiddate(new Date());
    order.setPayState(1);
    order.setPlusPrice(submitInfo.getPlusPrice());
    order.setReceiverAddress(submitInfo.getReceiverAddress());
    order.setReceiverMobile(submitInfo.getReceiverPhone());
    order.setTransportfee(submitInfo.getTransportfee());
    order.setTransportType(Integer.parseInt(submitInfo.getTransportType()));
    order.setSpecialOffer(submitInfo.getSpecialOffer());
    order.setTotalPrice(submitInfo.getTotalPrice());
    return orderMapper.updateById(order) == 1;
  }

  @Override
  public BaseOrdergoods imBuy(BaseOrdergoods dto) {
    BaseOrder order = new BaseOrder();
    order.setOrderCode(StringUtil.getUUID());
    BaseUser user = (BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest());
    order.setUserPhone(user.getPhone());
    order.setUserCode(user.getId());
    order.setUsername(user.getNickName());
    order.setOrderState("未提交");
    BaseUserrelation entity = new BaseUserrelation();
    entity.setUserCode(user.getId());
    relationMapper.selectOne(entity);
    order.setOperator(entity.getParent() + "");
    order.setPayState(1);
    orderMapper.insert(order);
    dto.setOrderCode(order.getOrderCode());
    dto.insert();
    return dto;
  }

  @Override
  public List<OrderGoodsVO> selectByCode(String orderCode) {
    return orderMapper.selectByCode(orderCode);
  }

  @Override
  public boolean passwordConfirm(Long id, String password) {
    return orderMapper.selectByPasswordConfirm(id, password) != null;
  }

  @Override
  public OrderDetailWithGoodsDto orderWithGoods(String orderCode) {
    OrderDetailWithGoodsDto dto = new OrderDetailWithGoodsDto();
    BaseOrder entity = new BaseOrder();
    entity.setOrderCode(orderCode);
    entity.setId(null);
    BaseOrder order = orderMapper.selectOne(entity);
    dto.setOrder(order);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("ORDERCODE", orderCode);
    List<BaseOrdergoods> list = ordergoodsMapper.selectByMap(map);
    dto.setList(list);
    return dto;
  }

  @Override
  public List<OrderGoodsVO> getOrderByCodeSubmit(String orderCode) {
    return orderMapper.getOrderByCodeSubmit(orderCode);
  }

  @Override
  public List<Count> countSale() {
    return orderMapper.countSale();
  }

  @Override
  public List<Count> countIncrease() {
    return orderMapper.countIncrease();
  }
}
